/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.client.view;

import javax.swing.JOptionPane;
import com.agenda.PrincipalCliente;
import com.agenda.client.ClienteServidor;
import com.agenda.client.ConfCliente;
import com.agenda.client.requisicoes.LoginRequisicao;
import com.agenda.model.RespostaRequisicao;
import com.agenda.model.Usuario;

/**
 *
 * @author Murilo
 */
public class LoginView extends javax.swing.JFrame {

	/**
	 * Creates new form LoginView
	 */
	public LoginView() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		emailText = new javax.swing.JTextField();
		servidorText = new javax.swing.JTextField();
		senhaText = new javax.swing.JPasswordField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		fecharButton = new javax.swing.JButton();
		conectarButton = new javax.swing.JButton();
		cadastrarButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Login");
		setResizable(false);
		getContentPane().setLayout(null);
		getContentPane().add(emailText);
		emailText.setBounds(60, 20, 200, 20);
		getContentPane().add(servidorText);
		servidorText.setBounds(60, 100, 140, 20);
		getContentPane().add(senhaText);
		senhaText.setBounds(60, 60, 90, 20);

		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel1.setText("email");
		jLabel1.setAlignmentY(1.0F);
		getContentPane().add(jLabel1);
		jLabel1.setBounds(30, 20, 30, 14);

		jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel2.setText("senha");
		getContentPane().add(jLabel2);
		jLabel2.setBounds(30, 60, 29, 14);

		jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel3.setText("Servidor");
		getContentPane().add(jLabel3);
		jLabel3.setBounds(20, 100, 40, 14);

		fecharButton.setText("Fechar");
		fecharButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				fecharButtonActionPerformed(evt);
			}
		});
		getContentPane().add(fecharButton);
		fecharButton.setBounds(100, 160, 80, 23);

		conectarButton.setText("Conectar");
		conectarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				conectarButtonActionPerformed(evt);
			}
		});
		getContentPane().add(conectarButton);
		conectarButton.setBounds(10, 160, 77, 23);

		cadastrarButton.setText("Cadastrar");
		cadastrarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cadastrarButtonActionPerformed(evt);
			}
		});
		getContentPane().add(cadastrarButton);
		cadastrarButton.setBounds(190, 160, 80, 23);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 303) / 2, (screenSize.height - 241) / 2, 303, 241);
	}// </editor-fold>//GEN-END:initComponents

	private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
		fechar();
	}//GEN-LAST:event_fecharButtonActionPerformed

	private void conectarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conectarButtonActionPerformed
		conectar();
	}//GEN-LAST:event_conectarButtonActionPerformed

	private void cadastrarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarButtonActionPerformed
		cadastrar();
	}//GEN-LAST:event_cadastrarButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cadastrarButton;
	private javax.swing.JButton conectarButton;
	private javax.swing.JTextField emailText;
	private javax.swing.JButton fecharButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPasswordField senhaText;
	private javax.swing.JTextField servidorText;

	// End of variables declaration//GEN-END:variables

	private void conectar() {

		ConfCliente conf = ConfCliente.getINSTANCIA();

		conf.put("email", emailText.getText());
		conf.put("servidor", servidorText.getText());

		ClienteServidor clienteServidor = getClienteServidor();

		clienteServidor.criarConexao();
		try {
			Usuario usuario = new Usuario();
			usuario.setLoginEmail(emailText.getText());
			usuario.setSenha(senhaText.getText());

			RespostaRequisicao resposta = clienteServidor.enviarInformacao(new LoginRequisicao(usuario));

			Boolean logou = clienteServidor.lerBoolean();
			if (logou) {
				PrincipalCliente.setUsuarioLogado(usuario);
				setVisible(false);

				AgendaView agenda = new AgendaView();
				clienteServidor.registrarOuvidorStatus(agenda);
				clienteServidor.addRecebimentoListener(agenda);
				agenda.setLocationRelativeTo(null);
				agenda.setVisible(true);
			} else {
				JOptionPane.showMessageDialog(this, "Usuário ou senha inválidos.");
			}

			//JOptionPane.showMessageDialog(this, "Dados recebidos do servidor :" + resposta);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Erro:" + e.getMessage());
		} finally {
			ClienteServidor.getINSTANCIA().liberarComunicacao();
		}

	}

	private ClienteServidor getClienteServidor() {
		ClienteServidor cliente = ClienteServidor.getINSTANCIA();
		cliente.setIp(servidorText.getText());
		cliente.setPorta(7000);
		return cliente;
	}

	public void carregarConfiguracoes() {
		ConfCliente conf = ConfCliente.getINSTANCIA();

		emailText.setText(conf.get("email"));
		servidorText.setText(conf.get("servidor"));
	}

	private void fechar() {
		System.exit(0);
	}

	private void cadastrar() {
		ClienteServidor clienteServidor = getClienteServidor();
		clienteServidor.criarConexao();
		CadastrarDialogView cadastrarDialog = new CadastrarDialogView(this, true);
		cadastrarDialog.setClienteServidor(clienteServidor);
		cadastrarDialog.setVisible(true);
	}
}
